clear


set seed 1073741823


/* First session */
insheet using "${pathdata_robustness}/RobustnessExperiment2_raw_wave1.csv", names

drop in 1/2

keep if writedowninfo =="2"

destring *, replace 

// ONLY NEEDED FOR BONUS PAYMENTS
//draw selected participants for bonus
randomselect, gen(sample) prop(0.1)
tab sample

// Randomly draw participants who would, if eligible, receive a bonus (30$) for for correct recall and for the belief stated in parts 1 and 2, respectively.
randomselect, gen(bonus_recall) prop(0.333)
randomselect if bonus_recall == 0, gen(bonus_belief) prop(0.5)

replace bonus_recall = . if sample == 0

replace bonus_belief = 2 if bonus_belief ==1
replace bonus_belief = 1 if bonus_belief ==0 & bonus_recall==0
replace bonus_belief = . if sample == 0


// Randomly draw product for which the bonus payment on beliefs (30$) is carried out, 

randomselect, gen(bicycle) prop(0.333)
randomselect if bicycle == 0, gen(restaurant) prop(0.5)

// Note 1: videogame; 2: bicycle; 3: restaurant
gen bonusproduct = ""
replace bonusproduct = "videogame" if bicycle ==0 & restaurant==0
replace bonusproduct = "bicycle" if bicycle ==1
replace bonusproduct = "restaurant" if restaurant ==1

/* Variables to keep */
keep condition_* p_guess* task* prolific_pid treatment bonusproduct bonus_belief bonus_recall truepos_fraction_* qid9 qid124 qid12 qid18 durationinseconds finished comprehension1 comprehension2 comprehension3 comprehension4 comprehension5 comprehension6 screenedout prolific_pid writedowninfo

	rename qid9 gender 

	rename qid124 age

	rename qid12 education

	rename qid18 employment

	* Indicator Male
	gen male = .
	replace male = 0 if inlist(gender, 2,3)
	replace male = 1 if gender == 1

	* Indicator Bachelor's degree or more
	gen college = .
	replace college = 0 if inlist(education, 1,2,3,4)
	replace college = 1 if inlist(education, 5,6,7,8)

	* Indicator Employment status
	gen employed = .
	replace employed = 0 if inlist(employment,3,4)
	replace employed = 1 if inlist(employment,1,2)

	* Indicator wave 1
	gen wave1 = 1
	
	* Indicator finished wave 1 - EMPLOYMENT IS THE LAST MANDATORY QUESTION IN THE SURVEY
	gen completed_wave1 = 0
	replace completed_wave1 = 1 if inlist(employment,1,2,3,4)


duplicates drop prolific_pid, force 

/* Wide to long */
reshape long condition_ p_guess_ task_ truepos_fraction_, i(prolific_pid) j(product) string

save "${pathdata_robustness}/temp/RobustnessExperiment2_long.dta", replace
*save "output/long1.dta", replace


duplicates drop prolific_pid, force
drop if prolific_pid==""
keep prolific_pid treatment

tab treatment 


save "${pathdata_robustness}/temp/RobustnessExperiment2_prolific_pid_only.dta", replace

clear


insheet using "${pathdata_robustness}/RobustnessExperiment2_raw_wave2.csv", names
drop in 1/2


/* Variables to keep */
keep beliefs_* recall_* valence_* prolific_pid screenedout durationinseconds
drop if prolific_pid == ""
sort prolific_pid
quietly by prolific_pid:  gen dup = cond(_N==1,0,_n)
drop if dup > 0
drop dup  

	* Indicator wave 1
	gen wave2 = 1


/* Wide to long */
reshape long beliefs_ recall_ valence_ cu_, i(prolific_pid) j(product) string

destring*, replace

save "${pathdata_robustness}/temp/RobustnessExperiment2_long_recall.dta", replace

/* Merged */
merge 1:1 prolific_pid product using "${pathdata_robustness}/temp/RobustnessExperiment2_long.dta"

* ########## CREATE DATA ON ATTRITION IN BETWEEN SUBJECT TREATMENTS ##########	

	
	* indicator whether subjects completed study 2
	gen completed = 0
	replace completed = 1 if _merge ==3
	
	** Treatments
	// 2. StoryStories | Target: Story. Decoys: 2x Story
	// 3. StoryStats | Target: Story. Decoys: 2x Statistic
	// 4  StoryNoInfos | Target: Story. Decoys: 2x No info
	// 5 StatStories | Target: Stat. Decoys: 2x Story
	// 6 StatStats | Target: Stat. Decoys: 2x Stat
	// 7 StatNoInfos | Target: Stat. Decoys: 2x NoInfo
	
	
	* betweeen subject treatment indicators
	gen decoy_stat = 0
	replace decoy_stat = 1 if inlist(treatment, 3,6)
	
	gen decoy_story = 0
	replace decoy_story = 1 if inlist(treatment, 2,5)
	
	* ##### Generate dataset with treatments and completion only
preserve
	* drop NAs
	drop if _merge==1

	keep prolific_pid decoy_stat decoy_story completed
	
	duplicates drop
	
	save "${pathdata_summary}/DecoyAttrition.dta", replace
	
restore
	
	drop decoy_stat decoy_story



keep if _merge==3
drop _merge

save "${pathdata_robustness}/temp/RobustnessExperiment2_long_combined.dta", replace



/* Exclusion Criteria */
drop if screenedout == "True"
drop if condition_ == "noinfo" & p_guess_ != 50

save "${pathdata_robustness}/temp/RobustnessExperiment2_merged.dta", replace



/* Auxiliary variables */
gen storyshort = 0
replace storyshort = 1 if condition == "storyshort_pos" || condition_ == "storyshort_neg"


/* Variables for analysis */

/* 1 - Effect variables */
replace beliefs_ = "88.65" if beliefs_=="88,65"
destring beliefs_, replace force

gen diff = 50 - p_guess_
gen diff_recall = 50 - beliefs_


gen effect = diff
replace effect = p_guess_ - 50 if condition == "storyshort_pos" || condition_ == "statistic_pos"

gen effect_recall = diff_recall
replace effect_recall = beliefs_ - 50 if condition == "storyshort_pos" || condition_ == "statistic_pos"


gen forgetting = (p_guess_  - beliefs_)/(p_guess_ - 50)

*c: formula on forgetting rates
*gen forgetting  = ( `var' -  f_`var')/( `var' - 50)


/* 2 - Correct recall */
gen correct_type = 0
replace correct_type =  1 if condition_=="noinfo" & recall_==1
replace correct_type =  1 if condition_=="statistic_pos" & recall_==2
replace correct_type =  1 if condition_=="statistic_neg" & recall_==2
replace correct_type =  1 if condition_=="storyshort_pos" & recall_==3
replace correct_type =  1 if condition_=="storyshort_neg" & recall_==3

/* 2 - Correct recall */
gen correct_valence = 0
replace correct_valence =  1 if inlist(condition_,"statistic_pos","storyshort_pos") & valence_==1
replace correct_valence =  1 if inlist(condition_,"statistic_neg","storyshort_neg") & valence_==2

/* 2 - Correct recall */
gen correct_recall = 0
replace correct_recall =  1 if correct_type == 1 & correct_valence == 1
replace correct_recall =  1 if correct_type == 1 & condition == "noinfo"

// gen decoy_valence = .
// sort task_
// bysort prolific_pid: replace decoy_valence =0 if valence_[1]==1 & valence_[2]==1 & valence_[3]==1 | valence_[1]==2 & valence_[2]==2 & valence_[3]==2
// bysort prolific_pid: replace decoy_valence =1 if valence_[1]==1 & valence_[2]==1 & valence_[3]==2 | valence_[1]==1 & valence_[2]==2 & valence_[3]==1
// bysort prolific_pid: replace decoy_valence =1 if valence_[1]==2 & valence_[2]==1 & valence_[3]==2 | valence_[1]==2 & valence_[2]==2 & valence_[3]==1
// bysort prolific_pid: replace decoy_valence =2 if valence_[1]==1 & valence_[2]==2 & valence_[3]==2 | valence_[1]==2 & valence_[2]==1 & valence_[3]==1

/* Drop missing products */
drop if p_guess_ == .

drop if valence_ == . & condition_ != "noinfo"

save "${pathdata_robustness}/RobustnessExperiment2.dta", replace




* ##### Save data for summary statistics

	keep prolific_pid male age college employed

	duplicates drop

	save "${pathdata_summary}/DecoySummary.dta", replace
